package minecrafter;

import java.applet.Applet;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Array;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.swing.JOptionPane;
import minecrafter.proxy.MineProxy;
import minecrafter.util.Resources;
import minecrafter.util.SimpleRequest;
import minecrafter.util.Streams;

public class MineClient extends Applet
{
  private static final long serialVersionUID = 1L;
  protected static float VERSION = 2.8F;
  protected static int proxyPort = 8061;
  protected static int proxyHTTPPort = 8062;
  protected static int proxyHTTPSPort = 8063;
  protected static String launcherDownloadURL = "https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft.jar";
  protected static String normalLauncherFilename = "Spoutcraft.jar";
  protected static boolean portableMode = false;

  public void init()
  {
    main(new String[0]);
  }

  public static void main(String[] args)
  {
    try
    {
      String auth = null;
      
      MineProxy.listen(proxyPort, proxyHTTPPort, proxyHTTPSPort, VERSION, auth);
      startLauncher();
      System.setProperty("http.proxyHost", "127.0.0.1");
      System.setProperty("http.proxyPort", Integer.toString(proxyPort));
      System.setProperty("https.proxyHost", "127.0.0.1");
      System.setProperty("https.proxyPort", Integer.toString(proxyPort));
    }
    catch (Exception e)
    {
      System.out.println("Something bad happened:"); e.printStackTrace(); System.exit(1);
    }
  }

  public static void startLauncher() {
    try
    {
      if (new File(normalLauncherFilename).exists())
      {
        URL u = new File(normalLauncherFilename).toURI().toURL();
        URLClassLoader cl = new URLClassLoader(new URL[] { u });

        Class launcherFrame = cl.loadClass("org.spoutcraft.launcher.Main");
        //Class utilClass = cl.loadClass("net.minecraft.Util");
        //Field portable = utilClass.getDeclaredField("portable");
        //portable.setBoolean(null, portableMode);
        Method main = launcherFrame.getMethod("main", Array.newInstance(String.class,0).getClass());
        main.invoke(launcherFrame, new Object[] { new String[0] });
      }
      else
      {
        try
        {
          InputStream in = new URL(launcherDownloadURL).openStream();
          OutputStream out = new FileOutputStream(normalLauncherFilename);
          Streams.pipeStreams(in, out);
          in.close();
          out.close();
          startLauncher();
        }
        catch (Exception ex)
        {
          System.out.println("Error downloading launcher:"); ex.printStackTrace(); return;
        }
      }
    }
    catch (Exception e1)
    {
      System.out.println("Error starting launcher:"); e1.printStackTrace();
    }
  }
}
